const connection = require('../app/database')

class likeService {
    async create(postId, userId) {
        try {
            const statement = `INSERT INTO article_like(article_id, user_id) VALUES(?, ?);`;
            const [result] = await connection.execute(statement, [postId, userId])
            return result
        } catch (error) {
            console.log('likeServiceCreate', error)
        }
    }

    async detele(postId, userId) {
        try {
            const statement = `DELETE FROM article_like WHERE article_id = ? AND user_id = ?;`;
            const [result] = await connection.execute(statement, [postId, userId])
            console.log('result',result)

            return result
        } catch (error) {
            console.log('likeServiceCreate', error)
        }
    }

    async followedList(userId) {
        try {
            const statement = `SELECT u.id, u.avatar, u.username, u.gender, u.birthday, u.desc, u.reg_time 
        FROM 
            users u 
            JOIN follow f ON u.id = f.followerId 
        WHERE 
            f.followedId = ?;`;
            const [result] = await connection.execute(statement, [userId])
            console.log('result',result)

            return result
        } catch (error) {
            console.log('likeServiceCreate', error)
        }
    }

    async followerList (userId) {
        try {
            const statement = `SELECT u.id, u.avatar, u.username, u.gender, u.birthday, u.desc, u.reg_time 
        FROM 
            users u 
            JOIN follow f ON u.id = f.followedId 
        WHERE 
            f.followerId = ?;`;
            const [result] = await connection.execute(statement, [userId])
            console.log('result',result)

            return result
        } catch (error) {
            console.log('likeServiceCreate', error)
        }
    }

}

module.exports = new likeService()
